package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.*;
import net.wanho.mapper.UserInfoMapper;
import net.wanho.po.UserInfo;
import net.wanho.service.UserInfoService;
import net.wanho.vo.AjaxResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * Author: Lun
 * Date: 2025-07-24 09:12
 * Description: <描述>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserInfoController {
    private final UserInfoService userInfoService;


    @GetMapping("/auth-list")
    public AjaxResult authList() {
        return AjaxResult.success(userInfoService.queryAuthList());
    }

    @GetMapping("/user-list")
    public AjaxResult userList(QueryUserListDto queryUserListDto,
                               @RequestParam(required = false) int pageNum,
                               @RequestParam(required = false) int pageSize) {
        return AjaxResult.success(userInfoService.queryPage(queryUserListDto, pageNum, pageSize));
    }

    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody AddUserDto addUserDto) {
        userInfoService.saveUser(addUserDto);
        return AjaxResult.success("用户添加成功!");
    }

    @PutMapping("/updateState")
    public AjaxResult updateState(@RequestBody UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return AjaxResult.success("用户状态修改成功!");
    }

    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable Integer id) {
        return AjaxResult.success(userInfoService.getById(id));
    }

    @PutMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody modifyUserDto modifyUserDto) {
        userInfoService.modifyById(modifyUserDto);
        return AjaxResult.success("用户修改成功!");
    }

    @DeleteMapping("/deleteUser/{id}")
    public AjaxResult deleteUser(@PathVariable Integer id) {
        userInfoService.removeUserById(id);
        return AjaxResult.success("用户删除成功!");
    }

    @DeleteMapping("/deleteUserList")
    public AjaxResult deleteUserList(@RequestBody Integer[] ids) {
        userInfoService.removeUserByIds(ids);
        return AjaxResult.success("用户批量删除成功!");
    }

    @PutMapping("/updatePwd/{id}")
    public AjaxResult updatePwd(@PathVariable Integer id) {
        userInfoService.updatePwd(id);
        return AjaxResult.success("密码重置成功!");
    }

    @GetMapping("/user-role-list/{id}")
    public AjaxResult userRoleList(@PathVariable Integer id) {
        return AjaxResult.success(userInfoService.queryRoleById(id));
    }

    @PutMapping("/assignRole")
    public AjaxResult assignRole(@RequestBody UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return AjaxResult.success("角色分配成功!");
    }

    @GetMapping("/user-auth")
    public AjaxResult userAuth(Integer userId) {
        return AjaxResult.success(userInfoService.queryAuthIds(userId));
    }

    @GetMapping("/exportTable")
    public AjaxResult exportTable(QueryUserListDto queryUserListDto,
                                  @RequestParam(required = false) Integer pageNum,
                                  @RequestParam(required = false) Integer pageSize,
                                  HttpServletResponse response) {
        return AjaxResult.success(userInfoService.exportTable(queryUserListDto, pageNum, pageSize, response));
    }

    @PutMapping("/modifyPwd")
    public AjaxResult modifyPwd(@RequestBody ModifyPwdDto modifyPwdDto) {
        userInfoService.modifyPwd(modifyPwdDto);
        return AjaxResult.success("密码修改成功!");
    }

    @PutMapping("/modifySelf")
    public AjaxResult modifySelf(@RequestBody ModifySelfDto modifySelfDto) {
        userInfoService.modifySelf(modifySelfDto);
        return AjaxResult.success("信息修改成功!");
    }

    @PostMapping("/modifyAvatar/{id}")
    public AjaxResult modifyAvatar(@PathVariable Integer id, MultipartFile file){
        String url = userInfoService.modifyAvatar(id, file);
        return AjaxResult.success("上传成功", url);
    }


}
